package cn.flexui.flexuiserver.controller.web;

import cn.dev33.satoken.stp.StpUtil;
import cn.flexui.flexuiserver.common.ResponseResultGenerator;
import cn.flexui.flexuiserver.config.properties.StripeProperties;
import cn.flexui.flexuiserver.domain.ResponseResult;
import cn.flexui.flexuiserver.domain.constant.PaymentConts;
import cn.flexui.flexuiserver.domain.dto.PaymentDTO;
import cn.flexui.flexuiserver.domain.entity.MemberCard;
import cn.flexui.flexuiserver.domain.entity.PaymentInfo;
import cn.flexui.flexuiserver.service.MemberCardService;
import cn.flexui.flexuiserver.service.PaymentInfoService;
import cn.flexui.flexuiserver.utils.IdGenerate;
import cn.flexui.flexuiserver.utils.MathUtils;
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.PaymentIntent;
import com.stripe.param.PaymentIntentCreateParams;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;

@Tag(name = "支付")
@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Autowired
    private MemberCardService memberCardService;
    @Autowired
    private StripeProperties stripeProperties;
    @Autowired
    private PaymentInfoService paymentInfoService;



    @Operation(summary = "创建支付订单")
    @PostMapping("/createPaymentIntent")
    public ResponseResult<String> createPaymentIntent(@RequestBody PaymentDTO paymentDTO) throws StripeException {

        long userId = StpUtil.getLoginIdAsLong();
        Long memberCardId = paymentDTO.getMemberCardId();
        MemberCard memberCard = memberCardService.getById(memberCardId);
        BigDecimal amount = memberCard.getAmount();
        String cardName = memberCard.getCardName();
        Long id = memberCard.getId();
        String paySn = IdGenerate.getId();
        Stripe.apiKey = stripeProperties.getApiKey();
        PaymentIntentCreateParams params = PaymentIntentCreateParams.builder()
                .setAmount(MathUtils.toFen(amount))
                .putMetadata("paySn",paySn)
                .setCurrency("usd")
//                .setPaymentMethod("")
//                .setConfirmationMethod(PaymentIntentCreateParams.ConfirmationMethod.MANUAL)
                .setAutomaticPaymentMethods(PaymentIntentCreateParams.AutomaticPaymentMethods.builder().setEnabled(true).build())
                .build();
        PaymentIntent paymentIntent = PaymentIntent.create(params);
        String paymentIntentId = paymentIntent.getId();

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setUserId(userId);
        paymentInfo.setPaySn(paySn);
        paymentInfo.setGoodsName(cardName);
        paymentInfo.setGoodsCode(id.toString());
        paymentInfo.setGoodsType(10);
        paymentInfo.setOutPaySn(paymentIntentId);
        paymentInfo.setPayAmount(amount);
        paymentInfo.setPayState(PaymentConts.PayStateEnum.PAYING.getState());
        paymentInfoService.save(paymentInfo);
        return ResponseResultGenerator.success(paymentIntent.getClientSecret());

    }

}
